package morphage.engine;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;

public class ImageManager
{
  private static Toolkit          s_toolkit = null;
  
  private static Image            s_firstImage  = null;
  private static Image            s_lastImage   = null;
  private static Image            s_curResult   = null;
  
  private synchronized static Image loadImage( String path ) throws FileNotFoundException
  {
    Image image = null;
    File file = new File(path);
    if( file.exists() )
      image = getToolkit().getImage(file.getAbsolutePath());
    else
      image = getToolkit().getImage(ClassLoader.getSystemResource(path));
    
    if( image == null )
      throw new FileNotFoundException( path );
    
    return image;
  }
  
  public synchronized static void loadFirstImage( String path ) throws FileNotFoundException
  {
    Image image = null;
    
    image = loadImage( path );
    
    s_firstImage = image;
  }
  
  public synchronized static void loadLastImage( String path ) throws FileNotFoundException
  {
    Image image = null;
    
    image = loadImage( path );
    
    s_lastImage = image;
  }
  
  private static Toolkit getToolkit()
  {
    if (s_toolkit == null)
      s_toolkit = Toolkit.getDefaultToolkit();
    return s_toolkit;
  }
  
  public synchronized static Image getFirstImage() { return s_firstImage; }
  public synchronized static Image getLastImage() { return s_lastImage; }
  public synchronized static Image getCurrentResult() { return s_curResult; }
  public synchronized static void setCurrentResult( Image img ) { s_curResult = img; }
}
